#!/bin/bash

source /home/shell/util.sh



# 安装函数
function install () {
    requireJava
    requireMaven
    source /etc/profile.d/java.sh
    while [ true ]; do
        printInfo "版本列表"
        curl -sL https://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/|grep -Po "\d+\.\d+\.\d+"|uniq
        read -p "请选择版本：" version
        if [[ -z "${version}" ]]; then
            printFail "请输入版本"
        else
            break;
        fi
    done
    download /opt/rocketmq.zip https://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/${version}/rocketmq-all-${version}-bin-release.zip
    unzip -d /opt /opt/rocketmq.zip
    rm -rf /opt/rocketmq.zip
    mv /opt/rocketmq* /opt/rocketmq
    
    if [ ! -d "~/store/commitlog" ]; then
        mkdir -p ~/store/commitlog
    fi
    if [ ! -d "~/store/consumequeue" ]; then
        mkdir -p ~/store/consumequeue
    fi
    
    while [ true ]; do
        printInfo "版本列表"
        curl -sL https://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/rocketmq-dashboard/|grep -Po "\d+\.\d+\.\d+"|uniq
        read -p "请选择版本：" version
        if [[ -z "${version}" ]]; then
            printFail "请输入版本"
        else
            break;
        fi
    done
    download /opt/rocketmq-dashboard.zip https://mirrors.tuna.tsinghua.edu.cn/apache/rocketmq/rocketmq-dashboard/${version}/rocketmq-dashboard-${version}-source-release.zip
    unzip -d /usr/local/src/ /opt/rocketmq-dashboard.zip
    rm -rf /opt/rocketmq-dashboard.zip
    mv /usr/local/src/rocketmq-dashboard-* /usr/local/src/rocketmq-dashboard/
    cd /usr/local/src/rocketmq-dashboard/
    sed -ri "s/rocketmq.config.namesrvAddr=/rocketmq.config.namesrvAddr=127.0.0.1:9876/g" /usr/local/src/rocketmq-dashboard/src/main/resources/application.properties
    mvn clean package -Dmaven.test.skip=true
    mkd /opt/rocketmq-dashboard/
    cp -r /usr/local/src/rocketmq-dashboard/target/rocketmq-dashboard-*.jar /opt/rocketmq-dashboard/rocketmq-dashboard.jar
}

function config () {
    sed -ri "/export ROCKETMQ_HOME/a export JAVA_HOME=/opt/jdk" /opt/rocketmq/bin/mqbroker
    sed -ri "/export ROCKETMQ_HOME/a export JAVA_HOME=/opt/jdk" /opt/rocketmq/bin/mqnamesrv
    firewall-addport 8080
    firewall-addport 9876
}


function run () {

bash /home/shell/serviced-install.sh<<EOF



mqbroker
bash /opt/rocketmq/bin/mqbroker





EOF

    while [ true ]; do
        if [ ! -f "/root/logs/rocketmqlogs/broker.log" ]; then
            sleep 1s
            loadMsg "系统启动中，请稍后..."
        else
            break;
        fi
        
    done


bash /home/shell/serviced-install.sh<<EOF



mqnamesrv
bash /opt/rocketmq/bin/mqnamesrv





EOF   

printInfo "begin"

bash /home/shell/serviced-install.sh<<EOF



mqdashboard
/opt/jdk/bin/java -Dspring.profiles.active=test -jar /opt/rocketmq-dashboard/rocketmq-dashboard.jar





EOF

printInfo "end"

    printInfo "==========================rocketmq=========================="
    printInfo "rocketmq日志目录：~/logs/rocketmqlogs/"
    printInfo "name server端口：9876"
    printInfo "控制台地址：http://${host_addr}:8080/"
    printInfo "==========================rocketmq=========================="

}

# 运行安装和配置
install && config && run